package demo;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-09-17
 * Time: 13:39
 */
public class Test {
    // 栈的压入弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && stack.peek() == popV[j] && j < popV.length) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens) {
            if(!isOpera(s)) {
                stack.push(Integer.parseInt(s));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public boolean isOpera(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return true;
        }
        return false;
    }

    // 栈的括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }else {
                    // 匹配
                    char tmp = stack.peek();
                    if(tmp == '(' && ch == ')' || tmp == '{' && ch == '}' || tmp == '[' && ch == ']') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
    // 判断环的入口点
    public ListNode detectCycle(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    // 判断链表带环
    public boolean hasCycle(ListNode head) {
        if(head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;

    }
    // 判断链表并求出相交节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) {
            return null;
        }
        ListNode pLong = headA;
        ListNode pShort = headB;
        int len1 = 0;
        int len2 = 0;
        while(pLong != null) {
            len1++;
            pLong = pLong.next;
        }
        while(pShort != null) {
            len2++;
            pShort = pShort.next;
        }
        pLong = headA;
        pShort = headB;
        // 求交点之前的差值
        int len = len1 - len2;
        if(len < 0) {
            len = -len;
            pLong = headB;
            pShort = headA;
        }
        // 走差值步
        while(len != 0) {
            pLong = pLong.next;
            len--;
        }
        while(pLong != pShort) {
            pLong = pLong.next;
            pShort = pShort.next;
        }
        return pLong;

    }

    // 删除数组中的所有元素
    public int removeElement(int[] nums, int val) {
        int j = 0;
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] != val){
                nums[j] = nums[i];
                j++;
            }
        }
        return j;
    }

    // 删除数组中的重复项
    public int removeDuplicates(int[] nums) {
        int i = 1;
        int j = 1;
        for(; i < nums.length; i++){
            if(nums[i] != nums[i-1]) {
                nums[j] = nums[i];
                j++;
            }
        }
        return j;
    }
    // 合并两个有序数组
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int end1 = m - 1;
        int end2 = n - 1;
        int end = m + n -1 ;
        while(end1 >= 0 && end2 >= 0) {
            if(nums1[end1] < nums2[end2]) {
                nums1[end--] = nums2[end2--];
            }else {
                nums1[end--] = nums1[end1--];
            }
        }
        while(end2 >= 0) {
            nums1[end--] = nums2[end2--];
        }
    }
    // 判断回文串
    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode slow = A;
        ListNode fast = A;
        // 1. 找中间节点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        // 2. 逆置
        ListNode cur = slow.next;
        slow.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 3. 判断
        ListNode head = A;
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }else {
                head = head.next;
                slow = slow.next;
                // 判断偶数节点
                if(head.next == slow) {
                    return true;
                }
            }
        }
        return true;
    }

    // 链表分割
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null) {
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = cur;
                }
            }else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as == null) {
            return bs;
        }else {
            ae.next = null;
        }
        return bs;
    }
    // 合并两个有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null && list2 != null) {
            return list2;
        }
        if(list1 != null && list2 == null) {
            return list1;
        }
        if(list1 == null && list2 == null) {
            return null;
        }
        ListNode newHead = new ListNode();
        ListNode head = newHead;
        while(list1 != null && list2 != null) {
            if(list1.val < list2.val) {
                head.next = list1;
                head = list1;
                list1 = list1.next;
            }else {
                head.next = list2;
                head = list2;
                list2 = list2.next;
            }
        }
        if(list1 == null) {
            head.next = list2;
        }
        if(list2 == null) {
            head.next = list1;
        }
        return newHead.next;
    }
    // 求链表的倒数第k个节点
    public ListNode findKthToTail(ListNode head, int k) {
        if(k <= 0) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1. 先让fast指针先走
        while(k - 1 != 0) {
            fast = fast.next;
            if(fast == null) {
                return null;
            }
            k--;
        }
        //2. 然后fast和slow一起走
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    // 寻找链表中间节点
    public ListNode middleNode(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    // 翻转链表
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    // 删除链表中的所有元素
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while(cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;

            }else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        if(head.val == val) {
            head = head.next;
        }
        return head;
    }


    public static void main(String[] args) {
        String s = "111";
        Integer.parseInt(s);
    }
}
